package com.example.demo.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.SInventory;
import com.example.demo.common.utils.Response;
import com.example.demo.mapper.SInventoryMapper;
import com.example.demo.service.SInventoryService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "库存信息服务接口")
@RestController
@RequestMapping("/inventory")
public class SIventoryController {
	@Autowired
	private SInventoryService siSrevice;
	@GetMapping("/all")
	@ApiOperation(value = "分页显示所有的库存信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1"),	@ApiImplicitParam(name="pageSize",value = "每页显示个数",defaultValue = "3")	
	})
	public Response findAll(@RequestParam(name="pageNum" ,defaultValue = "1")int pageNum ,@RequestParam(name="pageSize" ,defaultValue = "3")int pageSize){
		PageInfo<SInventory> page=siSrevice.findAllByPage(pageNum, pageSize);
		return Response.success(page);
	}
	@GetMapping("by/condition")
	@ApiOperation(value = "根据条件查找所有库存信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "productId",value = "产品编号"),
		@ApiImplicitParam(name = "warehouseId",value = "仓库编号"),
		@ApiImplicitParam(name="pageNum",value = "页码"),
		@ApiImplicitParam(name = "pageSize",value = "每页个数")
	})
	public Response findByCondition(Integer warehouseId,Integer productId,
			@RequestParam(defaultValue ="1")int pageNum,
			@RequestParam(defaultValue = "3")int pageSize) {
		PageInfo<SInventory> page=siSrevice.findByCondition(productId, warehouseId, pageNum, pageSize);
		return Response.success(page);
	}
	@PostMapping("saveorupdate")
	@ApiOperation(value = "保存或更新库存信息")
	public Response saveOrUpdate(SInventory si) {
		siSrevice.saveOrUpdate(si);
		return Response.success((si.getProductId()==null&&si.getWarehouseId()==null)?"添加库存信息成功":"保存库存信息成功");
	}
	@DeleteMapping("/delete")
	@ApiOperation("删除信息")
	public Response delete(Integer warehouseId,Integer productId) {
		siSrevice.delete(warehouseId, productId);
		return Response.success("删除成功");
	}
	@DeleteMapping("/batch/del")
	@ApiOperation(value = "批量删除")
	public Response batchDelete(int [] warehouseId,int [] productId) {
		siSrevice.batchDelete(warehouseId, productId);
		return Response.success("删除成功");
	}
}
